package com.example.spidertp.core.controller;

import com.example.spidertp.common.ResponseEntity;
import com.example.spidertp.common.annotation.OperationAspect;
import com.example.spidertp.core.model.vo.SpiderThreadPoolArgsVo;
import com.example.spidertp.core.model.vo.SpiderThreadPoolLivingStateVo;
import com.example.spidertp.core.service.SpiderTPService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2022/9/24 10:38 上午
 */

@Api(tags = "线程池管理控制器")
@RequestMapping("/api/spider-tp")
@RestController
public class SpiderTPController {

    @Autowired
    private SpiderTPService spiderTPService;

    @ApiOperation("获取当前活动线程池列表")
    @GetMapping("/list")
    public ResponseEntity<List<SpiderThreadPoolLivingStateVo>> getThreadPollArgs() {
        return ResponseEntity.ok(spiderTPService.selectList());
    }

    @ApiOperation("创建线程池")
    @PostMapping("/instance")
    @OperationAspect(accessPath = "/api/spider-tp/instance", accessDesc = "创建线程池")
    public ResponseEntity<SpiderThreadPoolArgsVo> createSpiderTp(String name){
        return ResponseEntity.ok(spiderTPService.createSpiderTp(name));
    }

    @ApiOperation("终止此线程")
    @PostMapping("/shutdown")
    @OperationAspect(accessPath = "/api/spider-tp/shutdown", accessDesc = "终止此线程")
    public ResponseEntity<Boolean> removeSpiderTp(String name){
        return ResponseEntity.ok(spiderTPService.removeSpiderTp(name));
    }

}
